<template>
  <div>
    <!-- 条件查询 -->
    <div class="block">
      <span class="demonstration">拼团状态</span>
      <el-select v-model="searchEntity.status" placeholder="请选择">
        <el-option label="请选择" value=""></el-option>
        <el-option label="关闭" :value="0"></el-option>
        <el-option label="开启" :value="1"> </el-option>
      </el-select>
      &emsp;&emsp;&emsp;
      <span class="demonstration">商品搜索:</span>
      <el-input
        placeholder="请输入商品名称、ID"
        style="width: 250px"
        v-model="searchEntity.msg"
      ></el-input>
      <el-button type="primary" round @click="getGroupGoodsList()"
        >查询</el-button>
    </div>

    <!-- 表格 -->
    <template>
      <el-table :data="groupGoodsList" border style="width: 100%">
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="image" label="拼团图片">
           <template slot-scope="scope">
            <img :src="scope.row.image" alt="未加载到图片" width="100px" height="100px" />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="拼团名称"> </el-table-column>
        <el-table-column prop="goodsScore" label="原价"> </el-table-column>
        <el-table-column prop="price" label="拼团价"> </el-table-column>
        <el-table-column prop="personNum" label="拼团人数"></el-table-column>
        <el-table-column prop="joinNum" label="参与人数"></el-table-column>
        <el-table-column prop="successNum" label="成团数量"></el-table-column>
        <el-table-column prop="sumLimit" label="限量"></el-table-column>
        <el-table-column prop="limitResidue" label="剩余限量"></el-table-column>
        <el-table-column prop="endTime" label="结束时间"></el-table-column>
       <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="rgba(67, 129, 230, 1)" inactive-color="#ff4949"
            active-value="1" inactive-value="0" @change="changeStatus($event,scope.row,scope.$index)">
          </el-switch>
        </template>
      </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="reply(scope.row.id)" type="text" size="small">编辑</el-button>
            <el-button @click="del(scope.row.id)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

        <!-- 分页 -->
    <div class="block">
      <el-pagination @size-change="reloadList(num)" @current-change="reloadList(num)"
        :current-page.sync="searchEntity.page" :page-sizes="[10, 20, 30, 40]" :page-size.sync="searchEntity.limit"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      token: "",
      groupGoodsList: [],
      total: 0,
      groupGoods:{},
      searchEntity: {
        limit: 10,
        page: 1,
      },
    };
  },
  //计算属性 类似于 data 概念
  computed: {
    headers () {
      return {
        "token": (localStorage.token || ""),
        "type": "1"
      }
    }
  },
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    del (id) {
      this.$http
        .delete(
          "http://localhost:8001/coco/group/goods/deleteGroupGoods/" + id, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        }
        )
        .then(({ data }) => {
          if (data.code == 0) {
            this.$message({
              message: '删除拼团商品成功!',
              type: 'success'
            });
          } else {
            this.$message.error('删除拼团商品失败!');
          }
          this.getGroupGoodsList();
        })
        .catch((err) => { });
    },
    reloadList (num) {
      //分页
      if (num == 1) {
        this.saleList();
      }
      if (num == 2) {
        this.warehouseList();
      }
      if (num == 3) {
        this.findSelloutGoods();
      }
      if (num == 4) {
        this.findRecycleGoods();
      }
    },
    getGroupGoodsList() {
      //列表
      //获取拼团商品列表
      this.$http
        .post(
          "http://localhost:8001/coco/group/goods/search",
          this.searchEntity,
          {
            headers: {
              token: this.token,
              type: "1",
            },
          }
        )
        .then(({ data }) => {
          console.log(data);
          if (data.code != 0) {
            this.$message({
              message: data.message,
              type: "error",
            });
            this.$router.push("/");
          }
          this.groupGoodsList = data.data.rows;
          // this.groupGoodsList.forEach((item) => {
          //   item.deployImg = JSON.parse(item.deployImg);
          //   console.log(item.deployImg);
          // });
          this.total = data.data.total;
        })
        .catch((err) => {});
    },
    changeStatus (data, row, index) {//上下架操作
      console.log(data, row, index);
      this.$http
        .put("http://localhost:8001/coco/group/goods/updateStatus/" + row.id + "/" + data, {}, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          console.log(data);
          if (data.code == 0) {
            this.$message({
              message: '修改成功!',
              type: 'success'
            });
            this.reloadList(this.num);
          } else {
            let newData = b;
            newData.publishStatus = newData.publishStatus == 1 ? "0" : "1";
            this.goodsList[index] = newData;
          }
        })
        .catch((err) => { });
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {
    this.token = localStorage.getItem("token");
    this.getGroupGoodsList();
  },
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style scoped>
</style>